
# setup
import numbers as np

from tensorflow import keras

from tensorflow.keras import layers

max_features = 20000
maxlen = 200

# build the model
# Input for variable-length sequences of integers
inputs = keras.Input(shape=(None,) ,dtype="int32")
# embed each integer in a 128-dimensional vector
x = layers.Embedding(max_features, 128)(inputs)
# Add 3 bidirectional LSTMs
x = layers.Bidirectional(layers.LSTM(64, return_sequences=True))(x)
# >>> inputs = tf.random.normal([32, 10, 8])
#   >>> lstm = tf.keras.layers.LSTM(4)
#   >>> output = lstm(inputs)
#   >>> print(output.shape)
x = layers.Bidirectional(layers.LSTM(64))(x)
# Add a classifier
# 二分类 最后一层一定要用sigmoid作为激活函数
outputs = layers.Dense(1,activation="sigmoid")(x)
model = keras.Model(inputs, outputs)
# 输出模型各层的参数情况
model.summary()

# Load data
(x_train,y_train), (x_val, y_val) = keras.datasets.imdb.load_data(num_words=max_features)
print(len(x_train), 'Training sequences')
print(len(x_val), 'Validation sequences')

x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen)
x_val = keras.preprocessing.sequence.pad_sequences(x_val, maxlen=maxlen)

# Train and evaluate
model.compile("adam", "binary_crossentropy", metrics=['accuracy'])
# 猜测应该是：执行完一个epoch, 然后在验证集上跑一次
model.fit(x_train, y_train, batch_size=32, epochs=2, validation_data=(x_val, y_val))
